package com.sirdc.modules.sys.web.threeSevenAndInterView;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.databean.BusinessSummary;
import net.huashitong.interview.entity.ProblemSummary;
import net.huashitong.interview.filter.SummaryRequestModel;
import net.huashitong.interview.service.SummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("web/summary")
public class WebSummaryController extends JsonBaseController {

    @Autowired
    private SummaryService service;
    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     *
     * 各县（市）区领导走访挂钩联系企业情况汇总表（tableType：1）
     *
     * @param model（fillDate【201807】、fillUnitId）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "querysummary",method = RequestMethod.POST)
    public Message querySummary(@RequestBody SummaryRequestModel model){
        if(ObjectUtils.isBlank(model.getFillUnitId())){
            model.setFillUnitId("1");
        }
        String fillDate = "";
        if(ObjectUtils.isBlank(model.getFillDate())){
            //获取数据库中最新的时间
            fillDate = service.getNewFillDate();
            model.setFillDate(fillDate.substring(0,6));
        }else{
            fillDate = model.getFillDate();
        }
        List<BusinessSummary> object = (List<BusinessSummary>) service.querySummary(model);
        if(ObjectUtils.isNotBlank(object)){
            BusinessSummary  businessSummary = object.get(0);
            Map<String,String> map = new HashMap<>();

            map.put("id",ObjectUtils.isNotBlank(businessSummary.getId())?businessSummary.getId():"-");
            map.put("fillDate",ObjectUtils.isNotBlank(businessSummary.getFillDate())?businessSummary.getFillDate():"-");
            map.put("year",ObjectUtils.isNotBlank(businessSummary.getFillDate())?businessSummary.getFillDate().substring(0,4):"-");
            map.put("month",ObjectUtils.isNotBlank(businessSummary.getFillDate())?businessSummary.getFillDate().substring(4,6):"-");
            map.put("fillUnitId",ObjectUtils.isNotBlank(businessSummary.getFillUnitId())?businessSummary.getFillUnitId():"-");
            map.put("allConnectBusiness",ObjectUtils.isNotBlank(businessSummary.getAllConnectBusiness())?businessSummary.getAllConnectBusiness():"-");
            map.put("businessThisMonth",ObjectUtils.isNotBlank(businessSummary.getBusinessThisMonth())?businessSummary.getBusinessThisMonth():"-");
            map.put("solvedProblem",ObjectUtils.isNotBlank(businessSummary.getSolvedProblem())?businessSummary.getSolvedProblem():"-");
            map.put("workingProblem",ObjectUtils.isNotBlank(businessSummary.getWorkingProblem())?businessSummary.getWorkingProblem():"-");
            map.put("effect",ObjectUtils.isNotBlank(businessSummary.getEffect())?businessSummary.getEffect():"-");
            map.put("helpExperience",ObjectUtils.isNotBlank(businessSummary.getHelpExperience())?businessSummary.getHelpExperience():"-");
            map.put("fillDate",fillDate);
            return coverMessage("200", "获取成功", map);
        }else{
            return coverMessage("200", "无数据");
        }
    }

    /**
     * 各县（市）区领导走访挂钩联系企业情况汇总表 添加功能
     * @param summary
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addsummary",method = RequestMethod.POST)
    public Message addSummary(@RequestBody ProblemSummary summary){
        service.save(summary);
        return coverMessage("200","添加成功");
    }
    /**
     * 各县（市）区领导走访挂钩联系企业情况汇总表 修改功能
     * @param summary
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editsummary",method = RequestMethod.POST)
    public Message editSummary(@RequestBody ProblemSummary summary){
        ProblemSummary newBean =  service.getById(summary.getSysId());
        if(ObjectUtils.isNotBlank(summary.getAllConnectBusiness())){
            newBean.setAllConnectBusiness(summary.getAllConnectBusiness());
        }
        if(ObjectUtils.isNotBlank(summary.getBusinessThisMonth())){
            newBean.setBusinessThisMonth(summary.getBusinessThisMonth());
        }
        if(ObjectUtils.isNotBlank(summary.getEffect())){
            newBean.setEffect(summary.getEffect());
        }
        if(ObjectUtils.isNotBlank(summary.getFillDate())){
            newBean.setFillDate(summary.getFillDate());
        }
        if(ObjectUtils.isNotBlank(summary.getFillUnitId())){
            newBean.setFillUnitId(summary.getFillUnitId());
        }
        if(ObjectUtils.isNotBlank(summary.getHelpExperience())){
            newBean.setHelpExperience(summary.getHelpExperience());
        }
        if(ObjectUtils.isNotBlank(summary.getSolvedProblem())){
            newBean.setSolvedProblem(summary.getSolvedProblem());
        }
        if(ObjectUtils.isNotBlank(summary.getWorkingProblem())){
            newBean.setWorkingProblem(summary.getWorkingProblem());
        }
        service.update(newBean);
        return coverMessage("200", "修改成功");
    }

    /**
     * 各县（市）区领导走访挂钩联系企业情况汇总表 删除功能
     * @param summary
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deletesummary")
    public Message deleteSummary(@RequestBody ProblemSummary summary){
        service.deleteById(summary.getSysId());
        return coverMessage("200", "删除成功");
    }
}
